package gmu.swe.project.query;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Contains a list of QueryResult(s) - each of which indicates the Peer, it's workload
 * and the full path to the file found.  Set may have more than one entry for a Peer
 * if more than one file matched the Query; however it should not have duplicates.  This
 * list should be displayed on the UI for the user to choose from.
 * 
 * @author Christina Bradbury
 *
 */
public class QueryResultSet implements Serializable {
	private static final long serialVersionUID = 1L;

	ArrayList<QueryResult> queryResultSet;
	
	public QueryResultSet() {
		queryResultSet = new ArrayList<QueryResult>();
	}

	/** Accessors **/
	public ArrayList<QueryResult> getQueryResultSet() {
		return queryResultSet;
	}

	public void setQueryResultSet(ArrayList<QueryResult> queryResultSet) {
		this.queryResultSet = queryResultSet;
	}
	
	//duplicate check needs to check all entries, not just name
	public void addQueryResult(QueryResult queryResult) {
		//need to remove current data if it's already in the set
		if(this.queryResultSet.contains(queryResult)) {
			this.queryResultSet.remove(queryResult);
		}
		this.queryResultSet.add(queryResult);
	}
	
	public void addQueryResultSet(QueryResultSet queryResultSet) {
		//don't add the results from the set if they are already part of the current set
		if(queryResultSet.getQueryResultSet() != null && queryResultSet.getQueryResultSet().size() != 0) {
			Iterator<QueryResult> i = queryResultSet.getQueryResultSet().iterator();
			while(i.hasNext()) {
				QueryResult result = (QueryResult)i.next();
				if(!this.queryResultSet.contains(result)) {
					this.queryResultSet.add(result);
				}
			}
		}
	}
	
	public int size() {
		return queryResultSet.size();
	}
}
